package com.itheima.tanhua.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.tanhua.api.BlackListApi;
import com.itheima.tanhua.api.QuestionApi;
import com.itheima.tanhua.api.SettingsApi;
import com.itheima.tanhua.api.UserApi;
import com.itheima.tanhua.domain.*;
import com.itheima.tanhua.utils.UserHolder;
import com.itheima.tanhua.vo.PageResult;
import com.itheima.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author Lff
 * @date 2022/3/25
 */
@Service
public class SettingsService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserApi userApi;

    /**
     * 保存用户设置的陌生问题
     *
     * @param content
     */
    public void saveQuestion(String content) {
        Question question = new Question();
        //设置用户id
        question.setUserId(UserHolder.getUser().getId());
        //设置正文内容
        question.setTxt(content);
        Question que = questionApi.findByUserId(question.getUserId());
        if (que == null) {
            //进行添加操作
            questionApi.save(question);
        } else {
            //修改
            question.setId(que.getId());
            questionApi.update(question);
        }

    }

    /**
     * 获取通用设置信息
     * @return
     */
    public SettingsVo getSettingsForId() {
        //获取用户id
        User user = UserHolder.getUser();
        //获取用户的陌生人问题
        Question question = questionApi.findByUserId(user.getId());
        //获取用户的通用设置
        Settings settings = settingsApi.findByUserId(user.getId());

        String txt = question == null ? "为什么添加我？" : question.getTxt();
        //设置返回对象模型
        SettingsVo settingsVo = new SettingsVo();
        if (settings != null) {
            settingsVo.setGonggaoNotification(settings.getGonggaoNotification());
            settingsVo.setLikeNotification(settings.getLikeNotification());
            settingsVo.setPinglunNotification(settings.getPinglunNotification());
        }
        //设置手机号
        settingsVo.setPhone(user.getMobile());
        //设置用户id
        settingsVo.setId(user.getId());
        //设置问题
        settingsVo.setStrangerQuestion(txt);
        return settingsVo;
    }

    /**
     * 更新用户设置
     * 存在就更新 不存在添加
     *
     * @param settings
     */
    public void updateSetting(Settings settings) {
        //获取用户id并填充
        settings.setUserId(UserHolder.getUser().getId());
        //查询
        Settings result = settingsApi.findByUserId(settings.getUserId());
        if (result == null) {
            settingsApi.save(settings);
        } else {
            //设置id
            settings.setId(result.getId());
            //更新
            settingsApi.updateById(settings);
        }
    }

    /**
     * 分页查询黑名单列表
     *
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult findBlackPage(Integer page, Integer pageSize) {
        //获取用户id
        Long id = UserHolder.getUser().getId();
        //调用api查询page
        IPage<UserInfo> iPage = blackListApi.findByUserIdToPage(id, page, pageSize);
        //封装到模型中
        PageResult pageResult = new PageResult(page, pageSize, iPage.getTotal(), iPage.getRecords());
        //返回
        return pageResult;
    }

    /**
     * 移除黑名单中的用户
     *
     * @param blackUserId
     */
    public void removeBlack(Long blackUserId) {
        //获取当前登录用户的id
        Long uid = UserHolder.getUser().getId();
        blackListApi.removeBlack(uid, blackUserId);
    }
}
